package com.crm.service.system.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.DTO.BusinessTaskDto;
import com.crm.DTO.RulePoolDto;
import com.crm.common.utils.ThreadLocalUtil;
import com.crm.entity.RulePool;
import com.crm.entity.system.SysNotice;
import com.crm.mapper.RulePoolMapper;
import com.crm.service.RulePoolService;
import com.crm.service.system.ISysNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RulePoolServiceImpl extends ServiceImpl<RulePoolMapper, RulePool> implements RulePoolService {


    //线索状态
    private static final int CLUE_POOL_Type = 0;

    //商机状态
    private static final int BUSINESS_POOL_Type = 1;

    private static final int TRANSFER = 1;

    //0-小时，1-天，2-周
    private static final int HOUR = 0;
    private static final int DAY = 1;
    private static final int WEEK = 2;


    @Autowired
    private RulePoolMapper rulePoolMapper;

    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 新增线索池
     *
     * @param rulePoolDto
     */
    @Override
    public void
    saveRulePool(RulePoolDto rulePoolDto) {
        //查看是否存在
        //先拿到所有的线索池规则
        List<RulePool> rulePools = rulePoolMapper.selectList(null);
        for (RulePool rulePool : rulePools) {
            if (rulePool.getType() == rulePoolDto.getType()) {
                updateByType(rulePoolDto);//已存在，执行更新操作
                return;
            }
        }
        RulePool rulePool = new RulePool();
        BeanUtils.copyProperties(rulePoolDto, rulePool);
        rulePoolMapper.insert(rulePool);
    }


    /**
     * 根据id查询线索池回显
     *
     * @param typeId
     * @return
     */
    @Override
    public RulePool getRulePoolByTypeId(Long typeId) {
        LambdaQueryWrapper<RulePool> eq = Wrappers.<RulePool>lambdaQuery().eq(RulePool::getType, typeId);
        return rulePoolMapper.selectOne(eq);
    }

    /**
     * 更新
     *
     * @param rulePoolDto
     */
    @Override
    public void updateByType(RulePoolDto rulePoolDto) {
        RulePool rulePool = new RulePool();
        BeanUtils.copyProperties(rulePoolDto, rulePool);

        LambdaQueryWrapper<RulePool> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RulePool::getType, rulePoolDto.getType());
        this.update(rulePool, wrapper);
    }

    /**
     * 获取回收时间
     * 返回值为小时
     *
     * @param businessType
     * @return
     */
    @Override
    public Integer getRecycleTime(Long businessType) {
        RulePool businessRule = getRulePoolByTypeId(businessType);
        Integer limitTime = businessRule.getLimitTime();
        Integer limitTimeType = businessRule.getLimitTimeType();
        return switch (limitTimeType) {
            case DAY -> limitTime * 24;
            case WEEK -> limitTime * 24 * 7;
            default -> limitTime;
        };
    }

    @Override
    public ArrayList<BusinessTaskDto> businessTaskList() {
        return rulePoolMapper.businessTaskList();
    }

    @Override
    public void businessToSea(ArrayList<BusinessTaskDto> businessToSea) {
        rulePoolMapper.businessToSea(businessToSea, TRANSFER);
    }

    /**
     * 商机转 公海通知
     *
     * @param businessToSea
     */
    @Override
    public void businessNotice(ArrayList<BusinessTaskDto> businessToSea) {
        //批量新增
        ArrayList<SysNotice> sysNotices = new ArrayList<>();
        for (BusinessTaskDto businessTaskDto : businessToSea) {
            SysNotice sysNotice = new SysNotice();
            sysNotice.setNoticeTitle("商机转公海");
            sysNotice.setNoticeType("4");
            sysNotice.setNoticeContent("商机【" + businessTaskDto.getId() + "】已转公海");
            sysNotice.setNoticeTo(businessTaskDto.getOwner());
            sysNotice.setStatus("0");
            sysNotices.add(sysNotice);
        }
        noticeService.saveBatch(sysNotices);
    }
   /**
     * 获取预警时间
     * 默认返回小时
     *
     * @param businessType
     * @return
     */
    @Override
    public Integer getWarnTime(Long businessType) {
        RulePool businessRule = getRulePoolByTypeId(businessType);
        Integer WarnTime = businessRule.getLimitTime();
        Integer WarnTimeType = businessRule.getLimitTimeType();
        return switch (WarnTimeType) {
            case DAY -> WarnTime * 24;
            case WEEK -> WarnTime * 24 * 7;
            default -> WarnTime;
        };
    }


}
